<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
   <script>
    var date = new Date();
    var template = "YYYY-MM-dd hh:mm:ss";
    var time = date.toLocaleString()
    var reg1 = /\d{4}/
    var times = '';
    function getTime(template, date) {
      if (template == 1) {
        var reg = /(\d{4})\/(\d{1,2})\/(\d{2})\s(\d{2}):(\d{2}):(\d{2})/;
        date.replace(reg, function (res, $1, $2, $3, $4, $5, $6) {
          $2 = $2 < 10 ? "0" + $2 : $2;
          return times = (`${$1}-${$2}-${$3} ${$4}:${$5}:${$6}`);
        });

      } else if (template == 2) {
        var reg = /(\d{2})\/(\d{1,2})\/(\d{2})\s(\d{2}):(\d{2}):(\d{2})/;
        date.replace(reg, function (res, $1, $2, $3, $4, $5, $6) {
          return times = (`${$4}:${$5} / ${$6}秒 ${$1}年${$2}月${$3}日`);
        });
      }
      return times;
    }
    var result1 = getTime(1, time);
    console.log(result1); 
    console.log("");
    var result2 = getTime(2, time);
    console.log(result2); 
   </script> 
</body>
</html>